package com.amorcloud.pig.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amorcloud.pig.R;
import com.amorcloud.pig.bean.UserInfoBean;
import com.amorcloud.pig.utils.UserDaoUtils;

public class ModifyPWDActivity extends BaseActivity implements View.OnClickListener {

    EditText mOldPwd, mNewPwd, mConfirmPwd;
    Button mSave, mCancel;
    long id;
    UserInfoBean currentUser;

    @Override
    protected void init() {
        setContentView(R.layout.activity_modify_pwd);
        initCustomToolbar(this);
        initView();
        initData();
    }

    private void initView() {
        mOldPwd = findViewById(R.id.etv_old_pwd);
        mNewPwd = findViewById(R.id.etv_new_pwd);
        mConfirmPwd = findViewById(R.id.etv_confirm_pwd);

        mSave = findViewById(R.id.btn_modify_pwd_save);
        mSave.setOnClickListener(this);
        mCancel = findViewById(R.id.btn_modify_pwd_cancle);
        mCancel.setOnClickListener(this);
    }

    private void initData() {
        id = getIntent().getLongExtra("userid", -1);
        currentUser = UserDaoUtils.queryById(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_modify_pwd_save:
                if(verifyPwd()) {
                    modifyPwd();
                }
                break;
            case R.id.btn_modify_pwd_cancle:
                this.finish();
                break;
            default:
                break;
        }
    }

    private boolean verifyPwd() {
        String oldPwd = mOldPwd.getText().toString();
        String newPwd = mNewPwd.getText().toString();
        String confirmPwd = mConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(confirmPwd)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(!oldPwd.equals(currentUser.getPasswrod())) {
            Toast.makeText(this, "原始密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(newPwd.length() < 6) {
            Toast.makeText(this, "密码长度不能小于6位", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(!newPwd.equals(confirmPwd)) {
            Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void modifyPwd() {
        UserInfoBean userInfoBean = new UserInfoBean();
        userInfoBean.setId(id);
        userInfoBean.setUserName(currentUser.getUserName());
        userInfoBean.setPasswrod(mNewPwd.getText().toString());
        userInfoBean.setIsAdmin(currentUser.getIsAdmin());
        UserDaoUtils.updateUser(userInfoBean);
        //clearUIData();
        Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
        this.finish();
    }

    private void clearUIData() {
        mOldPwd.setText("");
        mNewPwd.setText("");
        mConfirmPwd.setText("");
        mOldPwd.requestFocus();
    }
}
